$(function () {
    $("#jqGrid").Grid({
        url: '../planneedcheck/list',
        colModel: [
            {label: '需求名称', name: 'needName', index: 'NEED_NAME', width: 80},
            {label: '需求类型', name: 'needType', index: 'NEED_TYPE', width: 80},
            {label: '申请人', name: 'applyName', index: 'APPLY_USER', width: 80},
            {
                label: '申请时间', name: 'applyDate', index: 'APPLY_DATE', width: 80,
                formatter: function (value) {
                    return transDate(value, "yyyy-MM-dd");
                }
            },
            {label: '车间主管', name: 'deptCheckName', index: 'DEPT_CHECK_USER', width: 80},
            {
                label: '审批状态', name: 'deptCheckStatus', index: 'DEPT_CHECK_STATUS', width: 80,
                formatter: function (value) {
                    return sysEnums.CHECK_FLAG[value];
                }
            },
            {
                label: '审批时间', name: 'deptCheckDate', index: 'DEPT_CHECK_DATE', width: 80,
                formatter: function (value) {
                    return transDate(value, "yyyy-MM-dd");
                }
            },
            {label: '制造处', name: 'makeCheckName', index: 'MAKE_CHECK_USER', width: 80},
            {
                label: '审批状态', name: 'makeCheckStatus', index: 'MAKE_CHECK_STATUS', width: 80,
                formatter: function (value) {
                    return sysEnums.CHECK_FLAG[value];
                }
            },
            {
                label: '审批时间', name: 'makeCheciDate', index: 'MAKE_CHECI_DATE', width: 80,
                formatter: function (value) {
                    return transDate(value, "yyyy-MM-dd");
                }
            },
            {label: '综合评审人', name: 'leaderCheckName', index: 'LEADER_CHECK_USER', width: 80},
            {
                label: '审批状态', name: 'leaderCheckStatus', index: 'LEADER_CHECK_STATUS', width: 80,
                formatter: function (value) {
                    return sysEnums.CHECK_FLAG[value];
                }
            },
            {
                label: '审批时间', name: 'leaderCheckDate', index: 'LEADER_CHECK_DATE', width: 80,
                formatter: function (value) {
                    return transDate(value, "yyyy-MM-dd");
                }
            },
            {
                label: '是否结束', name: 'endFlag', index: 'END_FLAG', width: 80,
                formatter: function (value) {
                    return sysEnums.YES_OR_NO[value];
                }
            },
            {label: 'id', name: 'id', index: 'ID', key: true, hidden: true},
        ]
    });
});

var vm = new Vue({
    el: '#rrapp',
    data: {
        showList: true,
        title: null,
        planNeedCheck: {
            endFlag: '',
            leaderCheckDate: '',
            leaderCheckStatus: '',
            leaderCheckUser: '',
            makeCheciDate: '',
            makeCheckStatus: '',
            makeCheckUser: '',
            deptCheckDate: '',
            deptCheckStatus: '',
            deptCheckUser: '',
            applyDate: '',
            applyUser: '',
            needType: '',
            needName: '',
            id: '',
            updateDate: '',
            updateUser: '',
            createDate: '',
            createUser: '',
            delFlag: '',
        },
        ruleValidate: {
            name: [
                {required: true, message: '名称不能为空', trigger: 'blur'}
            ]
        },
        q: {
            name: ''
        }
    },
    methods: {
        deptCheck: function () {
            var data = getSelectedRowData("#jqGrid");

            if (data.id == null) {
                return;
            }

            if (data.deptCheckStatus !== '未审核') {
                alert("此记录已审核", function () {
                    vm.reload()
                });
                return;
            }

            openWindow({
                title: "确认审核",
                area: ['350px', '200px'],
                content: jQuery("#reviewLayer"),
                btn: ['确定', '取消'],
                btn1: function (index) {
                    if (typeof (vm.planNeedCheck.checkResult) == 'undefined') {
                        alert("请选择审核状态");
                        return;
                    }
                    vm.planNeedCheck = {deptCheckStatus: vm.planNeedCheck.checkResult, id: data.id};
                    var url = '../planneedcheck/deptCheck';
                    var params = JSON.stringify(vm.planNeedCheck);
                    vm.updateCheck(url, params)
                }
            });

        },
        makeCheck: function () {
            var data = getSelectedRowData("#jqGrid");

            if (data.id == null) {
                return;
            }

            if (data.deptCheckStatus !== '已审核') {
                alert("此记录未通过车间主管审核", function () {
                    vm.reload()
                });
                return;
            }

            if (data.makeCheckStatus !== '未审核') {
                alert("此记录已审核", function () {
                    vm.reload()
                });
                return;
            }

            openWindow({
                title: "确认审核",
                area: ['350px', '200px'],
                content: jQuery("#reviewLayer"),
                btn: ['确定', '取消'],
                btn1: function (index) {
                    if (typeof (vm.planNeedCheck.checkResult) == 'undefined') {
                        alert("请选择审核状态");
                        return;
                    }
                    vm.planNeedCheck = {makeCheckStatus: vm.planNeedCheck.checkResult, id: data.id};
                    var url = '../planneedcheck/makeCheck';
                    var params = JSON.stringify(vm.planNeedCheck);
                    vm.updateCheck(url, params)
                }
            });

        },
        leaderCheck: function () {
            var data = getSelectedRowData("#jqGrid");

            if (data.id == null) {
                return;
            }

            if (data.makeCheckStatus !== '已审核') {
                alert("此记录未通过制造处审核", function () {
                    vm.reload()
                });
                return;
            }

            if (data.leaderCheckStatus !== '未审核') {
                alert("此记录已审核", function () {
                    vm.reload()
                });
                return;
            }

            openWindow({
                title: "确认审核",
                area: ['350px', '200px'],
                content: jQuery("#reviewLayer"),
                btn: ['确定', '取消'],
                btn1: function (index) {
                    if (typeof (vm.planNeedCheck.checkResult) == 'undefined') {
                        alert("请选择审核状态");
                        return;
                    }
                    vm.planNeedCheck = {leaderCheckStatus: vm.planNeedCheck.checkResult, id: data.id};
                    var url = '../planneedcheck/leaderCheck';
                    var params = JSON.stringify(vm.planNeedCheck);
                    vm.updateCheck(url, params)
                }
            });

        },
        updateCheck: function (url, params) {
            Ajax.request({
                url: url,
                params: params,
                contentType: "application/json",
                type: 'POST',
                successCallback: function (res) {
                    alert('审核成功!');
                    setTimeout(function () {
                        location.reload();
                    }, 1000);
                }
            });
        },
        query: function () {
            vm.reload();
        },
        add: function () {
            vm.showList = false;
            vm.title = "新增";
            vm.planNeedCheck = {
                endFlag: 0,
                leaderCheckDate: '',
                leaderCheckStatus: '',
                leaderCheckUser: '',
                makeCheciDate: '',
                makeCheckStatus: '',
                makeCheckUser: '',
                deptCheckDate: '',
                deptCheckStatus: '',
                deptCheckUser: '',
                applyDate: '',
                applyUser: '',
                needType: '',
                needName: '',
                updateDate: '',
                updateUser: '',
                createDate: '',
                createUser: '',
                delFlag: '',
            };
        },
        update: function (event) {
            var id = getSelectedRow("#jqGrid");
            if (id == null) {
                return;
            }
            vm.showList = false;
            vm.title = "修改";

            vm.getInfo(id);
        },
        saveOrUpdate: function (event) {
            var url = vm.planNeedCheck.id == null ? "../planneedcheck/save" : "../planneedcheck/update";
            Ajax.request({
                url: url,
                params: JSON.stringify(vm.planNeedCheck),
                type: "POST",
                contentType: "application/json",
                successCallback: function (r) {
                    alert('操作成功', function (index) {
                        vm.reload();
                    });
                }
            });
        },
        del: function (event) {
            var ids = getSelectedRows("#jqGrid");
            if (ids == null) {
                return;
            }

            confirm('确定要删除选中的记录？', function () {
                Ajax.request({
                    url: "../planneedcheck/delete",
                    params: JSON.stringify(ids),
                    type: "POST",
                    contentType: "application/json",
                    successCallback: function () {
                        alert('操作成功', function (index) {
                            vm.reload();
                        });
                    }
                });
            });
        },
        getInfo: function (id) {
            Ajax.request({
                url: "../planneedcheck/info/" + id,
                async: true,
                successCallback: function (r) {
                    vm.planNeedCheck = r.planNeedCheck;
                }
            });
        },
        reload: function (event) {
            vm.showList = true;
            var page = $("#jqGrid").jqGrid('getGridParam', 'page');
            $("#jqGrid").jqGrid('setGridParam', {
                postData: {'name': vm.q.name},
                page: page
            }).trigger("reloadGrid");
            vm.handleReset('formValidate');
        },
        reloadSearch: function () {
            vm.q = {
                name: ''
            };
            vm.reload();
        },
        handleSubmit: function (name) {
            handleSubmitValidate(this, name, function () {
                vm.saveOrUpdate()
            });
        },
        handleReset: function (name) {
            handleResetForm(this, name);
        }
    }
});